// Fill out your copyright notice in the Description page of Project Settings.


#include "UI/WidgetController/AttributeMenuWidgetController.h"

#include "AbilitySystem/AuraAttributeSet.h"
#include "DataAsset/AttributeInfo.h"

void UAttributeMenuWidgetController::BroadcastInitialValues()
{
	Super::BroadcastInitialValues();

	UAuraAttributeSet* AS = Cast<UAuraAttributeSet>(AttributeSet);

	for (auto& Pair : AS->TagsToAttributesMap)
	{
		BroadcastAttributeInformation(Pair.Key, Pair.Value(), AS);
	}
}

void UAttributeMenuWidgetController::BroadcastAttributeInformation(
	const FGameplayTag& Tag,
	const FGameplayAttribute& Attribute,
	const UAuraAttributeSet* AuraAttributeSet
) const
{
	// 根据标签查找封装的属性信息
	FAttributeInformation AttributeInformation = AttributeInfo->FindAttributeInformationByTag(Tag);
	// 封装属性信息
	AttributeInformation.AttributeValue = Attribute.GetNumericValue(AuraAttributeSet);
	// 广播封装的属性信息
	AttributeInformationDelegate.Broadcast(AttributeInformation);
}

void UAttributeMenuWidgetController::BindCallbacksToDependencies()
{

	UAuraAttributeSet* AS = Cast<UAuraAttributeSet>(AttributeSet);
	// 将所有的属性变化委托都绑定到Lambda函数上
	for (auto& Pair : AS->TagsToAttributesMap)
	{
		AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(Pair.Value()).AddLambda(
			[this,Pair,AS](const FOnAttributeChangeData& Data)
			{
				// 传递属性标签、属性值、属性集, 广播属性信息,
				BroadcastAttributeInformation(Pair.Key, Pair.Value(), AS);
			}
		);
	}
}
